////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2009 Duykham.
// All Rights Reserved.
// Author: Duykham (duykham@gmail.com)
// Modified: 20090330
// TODO: load users information from XML file
//
////////////////////////////////////////////////////////////////////////////////
package dk.agrasys
{
	import flash.events.*;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IViewCursor;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	import mx.utils.ArrayUtil;

	/**
	 * Load sensors information from an XML file 
	 * @author Duykham
	 * 
	 */	
	public class LoadUser
	{
		private var service:HTTPService;

		/**
		 * Array contains information of users 
		 */        
        [Bindable]
		public var users:ArrayCollection;

		/**
		 * Constructor 
		 * @param url
		 * 
		 */		
		public function LoadUser(url:String)
		{
			service = new HTTPService();
			service.url = url;
			service.addEventListener(ResultEvent.RESULT, resultHandler);
			service.send();
		}

		/**
		 * Handle result when data is loaded  
		 * @param event
		 * 
		 */		
		private function resultHandler(event:ResultEvent):void
		{
		    var result:ArrayCollection = event.result.users.user is ArrayCollection
		        ? event.result.users.user as ArrayCollection
		        : new ArrayCollection(ArrayUtil.toArray(event.result.users.user));    
		    var temp:ArrayCollection = new ArrayCollection();
		    var cursor:IViewCursor = result.createCursor();
		    //var i:int =0;
		    while (!cursor.afterLast)
		    {
		    	//trace(i++);
		    	var u:Object = new User(cursor.current);
		    	//trace(u.username);
		        temp.addItem({username:u.username, 
		        			password:u.password, 
		        			permission:u.permission, 
		        			encrypt:u.encrypt}); 
		        /* temp.addItem({username:'duykham', 
		        			password:'duykham', 
		        			permission:'0', 
		        			encrypt:'false'}); */
		        cursor.moveNext();
		    }
		    users = temp;
		    //trace(users.length);
		}
	}
}